package nix.mail;

import java.io.File;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.junit.Test;

public class TestMail {

	@Test
	public void fun1() throws Exception {

		Properties props = new Properties();
		props.setProperty("mail.host", "smtp.126.com");
		props.setProperty("mail.smtp.auth", "true");

		Authenticator auth = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("lx-1203", "******");
			}
		};

		Session session = Session.getInstance(props, auth);

		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("lx-1203@126.com"));
		// 设置收件人
		msg.setRecipients(RecipientType.TO, "393400469@qq.com");
		// 设置抄送
		msg.setRecipients(RecipientType.CC, "berry.li@worldwayhk.com");
		// 设置密送
		msg.setRecipients(RecipientType.BCC, "243367675@qq.com");

		msg.setSubject("JavaMail测试邮件");
		msg.setContent("这是一封来自nixi1的JavaMail测试邮件！", "text/html;charset=utf-8");

		Transport.send(msg);
	}

	@Test
	public void fun2() throws Exception {

		Properties props = new Properties();
		props.setProperty("mail.host", "smtp.126.com");
		props.setProperty("mail.smtp.auth", "true");

		Authenticator auth = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("lx-1203", "******");
			}
		};

		Session session = Session.getInstance(props, auth);

		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("lx-1203@126.com"));
		msg.setRecipients(RecipientType.TO, "berry.li@worldwayhk.com");

		msg.setSubject("JavaMail测试邮件[附件]");

		MimeMultipart list = new MimeMultipart();

		MimeBodyPart part1 = new MimeBodyPart();
		part1.setContent("这是一封来自nixi1的JavaMail测试邮件[附件]！",
				"text/html;charset=utf-8");
		list.addBodyPart(part1);

		MimeBodyPart part2 = new MimeBodyPart();
		part2.attachFile(new File("d:/a.mp3"));
		// 使用MimeUtility.encodeText编码中文文件名
		part2.setFileName(MimeUtility.encodeText("中文名.mp3"));
		list.addBodyPart(part2);

		msg.setContent(list);

		Transport.send(msg);
	}

	@Test
	public void fun3() throws Exception {

		Session session = MailUtils.createSession("smtp.126.com", "******",
				"zx232323");
		Mail mail = new Mail("lx-1203@126.com",
				"berry.li@worldwayhk.com,243367675@qq.com",
				"MailUtils测试邮件[附件]", "这是一封来自nixi1的JavaMail测试邮件[附件]！");
		AttachBean ab1 = new AttachBean(new File("d:/a.mp3"), "中文名.mp3");
		AttachBean ab2 = new AttachBean(new File("d:/b.jpg"), "附件.jpg");
		mail.addAttach(ab1);
		mail.addAttach(ab2);
		MailUtils.send(session, mail);
	}
}
